package com.haoyun.mirage.user.model.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Lists;
import com.haoyun.common.core.jackson.serializer.LongJsonDeserializer;
import com.haoyun.common.core.jackson.serializer.LongJsonSerializer;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Created by twg on 2018/6/3.
 */
@Getter
@Setter
public class UserDTO implements Serializable {

    private static final long serialVersionUID = -955716126119448745L;
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long id;
    private Date gmtCreate;
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long compId;//公司ID
    private String userName;
    private String realName;
    private Integer userLevel;//用户级别
    private String salt;
    private String userPassword;
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long deptId;//部门ID
    private String deptName;
    @JsonSerialize(using = LongJsonSerializer.class)
    @JsonDeserialize(using = LongJsonDeserializer.class)
    private Long roleId;
    private String roleName;
    private String roleCode;
    private Integer isEnabled;
    private Integer isDeleted;

    //角色列表
    private List<RoleDTO> roles = Lists.newArrayList();

    //部门列表
    private List<DeptDTO> depts = Lists.newArrayList();

    /**
     * 角色id集合
     */
    private String[] roleIds;

    /**
     * 角色描述
     */
    private String roleDesc;

    public String getRoleDesc() {
        if (CollectionUtil.isNotEmpty(roles)) {
            final StringBuilder sb = new StringBuilder();
            boolean isFirst = true;
            for (RoleDTO role : roles) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    sb.append(StrUtil.COMMA);
                }
                sb.append(role.getRoleDesc());
            }
            return sb.toString();
        }
        return "";
    }

    public String[] getRoleIds() {
        if (CollectionUtil.isNotEmpty(roles)) {
            final List<String> roleIds = Lists.newArrayList();
            for (RoleDTO role : roles) {
                roleIds.add(role.getId().toString());
            }
            return roleIds.toArray(new String[roleIds.size()]);
        }
        return roleIds;
    }
}
